package com.supermap.https;

import org.junit.Test;

import javax.net.ssl.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;

/**
 * @author liqs
 * @version 1.0
 * @date 2020/8/27 11:38
 * 主要是为了测试 采用 域名的方式进行访问的限制
 */
public class test {

    @Test
    public void test() throws IOException {

        //  该测试案例直接加上是没有问题的
        disableSslVerification();

        // 连接到支付宝网站
//        URL url = new URL("https://110.75.244.16/gateway.do"); // openapi.alipay.com
        // 访问连接VPN后的数据访问
//        URL url = new URL("https://vpn.supermap.com/por/service.csp?showsvc=1&autoOpen=1&rnd=ahbdkjjalae"); // openapi.alipay.com
        URL url = new URL("https://1.202.165.43/por/service.csp?showsvc=1&autoOpen=1&rnd=ahbdkjjalae"); // openapi.alipay.com

        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
        // 新增部分
//        conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
        conn.connect();
        InputStream is = conn.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
        br.close();
        is.close();
    }

    // 定制Verifier
    public class TrustAnyHostnameVerifier implements HostnameVerifier {
        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    }

    // https与http不同的是，https加密，需要验证证书，而http不需要
    private static void disableSslVerification() {
        try
        {
            // Create a trust manager that does not validate certificate chains
            TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
                public void checkClientTrusted(X509Certificate[] certs, String authType) {
                }
                public void checkServerTrusted(X509Certificate[] certs, String authType) {
                }
            }
            };

            // Install the all-trusting trust manager
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

            // Create all-trusting host name verifier
            HostnameVerifier allHostsValid = new HostnameVerifier() {
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            };

            // Install the all-trusting host verifier
            HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        }
    }



}
